<?php

/**
 * Created by PhpStorm.
 * User: zhao shou qiang
 * Date: 2017\12\11
 * Time: 10:06
 */

namespace backend\controllers;

use common\models\MyPager;
use common\models\PackageUpdateLog;
use Yii;
use common\models\PackageLog;
use common\models\Responses;
use yii\data\Pagination;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\widgets\LinkPager;

/**
 * 资产包操作记录及资产包更新操作记录
 */
class PackageLogController extends BaseController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return parent::behaviors();
    }

    /**
     * 资产包日志
     * @return string
     */
    public function actionIndex()
    {
        //当前页
        $page = Yii::$app->request->post('page', 1);
        $packageId = Yii::$app->request->get('package_id');
        //$packageId = 11;
        //$userId = Yii::$app->user->id;

        //资产包日志
        $packageLog = new PackageLog();
        $packageLogs = $packageLog->getList($packageId);

        //资产包更新日志
        $packageUpdateLog = new PackageUpdateLog();
        $modules = $packageUpdateLog->getModuleArrayTitle();
        $packageUpdateLogUsers = $packageUpdateLog->getUsers($packageId);
        $count = $packageUpdateLog->getCount($packageId, 2, null, null);

        $offset = 20;//默认每页显示条数
        $date = 2;//默认一月内
        $packageUpdateLogs = $packageUpdateLog->getList($packageId, $date, null, null, $offset, ($page - 1) * $offset);

        //分页显示
        $param = array(
            'total_rows' => $count,
            'list_rows' => $offset, //每页显示条数
            'sub_pages' => 10, //预定显示页数
            'page_name' => 'page',
            'now_page' => $page,
        );
        $myPager = new MyPager($param);
        $pager = $myPager->contsruct_num_Page();

        return $this->render('index', [
            'packageLogs' => $packageLogs,
            'modules' => $modules,
            'packageUpdateLogUsers' => $packageUpdateLogUsers,
            //资产包修改记录日志
            'data' => [
                'count' => $count,
                'data' => $packageUpdateLogs,
                'page' => $page,
                'pager' => $pager,
                'package_id' => $packageId
            ]
        ]);
    }

    /**
     * ajax分页
     * @return array
     */
    public function actionPage()
    {
        if (Yii::$app->request->isAjax) {
            $packageId = Yii::$app->request->post('package_id');
            $page = Yii::$app->request->post('page', 1);//当前页
            $pageSize = Yii::$app->request->post('pageSize', 20);//每页条数

            $module = Yii::$app->request->post('module') ? (int)Yii::$app->request->post('module') : 0; //模型
            $user = Yii::$app->request->post('user') ? (int)Yii::$app->request->post('user') : 0; //当前用户
            $date = Yii::$app->request->post('date'); //查询时间

            //列表数据
            $packageUpageLog = new PackageUpdateLog();
            $count = $packageUpageLog->getCount($packageId, $date, $module, $user);
            $totalPage = ceil($count / $pageSize);
            //重设当前页
            if ($page < 1) {
                $page = 1;
            } elseif ($page > $totalPage) {
                $page = $totalPage;
            }
            $offset = ((int)$page - 1) * $pageSize;
            $list = $packageUpageLog->getList($packageId, $date, $module, $user, $limit = $pageSize, $offset);

            //分页数据
            $param = array(
                'total_rows' => $count,
                'list_rows' => $pageSize,
                'sub_pages' => 10,
                'page_name' => 'page',
                'now_page' => $page
            );
            $myPager = new MyPager($param);
            $pager = $myPager->contsruct_num_Page();

            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return Responses::response200(array('list' => $list, 'pager' => $pager, 'count' => count($list)), 'success', true);
        }
    }


    /**
     * ajax分页
     * @return array
     */
    public function actionPage2()
    {
        if (Yii::$app->request->isAjax) {
            $packageId = Yii::$app->request->get('package_id');
            $page = Yii::$app->request->get('page', 1);//当前页
            $pageSize = Yii::$app->request->get('pageSize', 10);//每页条数

            $module = Yii::$app->request->get('module') ? (int)Yii::$app->request->get('module') : 0; //模型
            $user = Yii::$app->request->get('user') ? (int)Yii::$app->request->get('user') : 0; //当前用户
            $date = Yii::$app->request->get('date'); //查询时间

            //列表数据
            $packageUpageLog = new PackageUpdateLog();
            $count = $packageUpageLog->getCount($packageId, $date, $module, $user);
            $totalPage = ceil($count / $pageSize);
            //重设当前页
            if ($page < 1) {
                $page = 1;
            } elseif ($page > $totalPage) {
                $page = $totalPage;
            }
            $offset = ((int)$page - 1) * $pageSize;
            $list = $packageUpageLog->getList($packageId, $date, $module, $user, $limit = $pageSize, $offset);

            //分页数据
            /*****************************************/
            $moduleArrayTitle = $packageUpageLog->getModuleArrayTitle();
            $oneWeekAgoDate = date("Y-m-d H:i:s", strtotime("-1 week"));    //一周前
            $oneMonthAgoDate = date("Y-m-d H:i:s", strtotime("-1 month"));    //一月前
            $currentDate = date('Y-m-d H:i:s', time());

            $where = ['package_id' => $packageId];
            $query = PackageUpdateLog::find()
                ->where($where);

            switch ($date) {
                //一周内
                case 1:
                    $query->andWhere(['between', 'created_at', $oneWeekAgoDate, $currentDate]);
                    break;
                //一月内
                case 2:
                    $query->andWhere(['between', 'created_at', $oneMonthAgoDate, $currentDate]);
                    break;
                //一月前
                case 3:
                    $query->andWhere(['<=', 'created_at', $oneMonthAgoDate]);
                    break;
                //默认一月内
                default:
                    $query->andWhere(['between', 'created_at', $oneMonthAgoDate, $currentDate]);
            }

            if ($user) {
                $query->andWhere(['user_id' => $user]);
            }

            if ($module) {
                $query->andWhere(['like', 'module', $moduleArrayTitle[$module]]);
            }

            $countQuery = clone $query;
            $count = $countQuery->count();
            $pages = new Pagination(['pageSize' => $pageSize, 'totalCount' => $count]);
            $linkpager = LinkPager::widget([
                'pagination' => $pages,
                'hideOnSinglePage' => false,
                'maxButtonCount' => 10, //默认显示10页
                'nextPageLabel' => '下一页',
                'prevPageLabel' => '上一页',
                'firstPageLabel' => '首页',
                'lastPageLabel' => '尾页',
                //'options' => ['class' => 'm-pagination'],
            ]);
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return Responses::response200(array('list' => $list, 'pager' => $linkpager, 'count' => $count, 'page' => $page), 'success', true);
        }
    }

}
